/*********************************************************
 *********************************************************
 ********************                  *******************
 *************                                ************
 *******                  _oo0oo_                  *******
 ***                     o8888888o                     ***
 *                       88" . "88                       *
 *                       (| -_- |)                       *
 *                       0\  =  /0                       *
 *                     ___/`---'\___                     *
 *                   .' \\|     |// '.                   *
 *                  / \\|||  :  |||// \                  *
 *                 / _||||| -:- |||||- \                 *
 *                |   | \\\  -  /// |   |                *
 *                | \_|  ''\---/''  |_/ |                *
 *                \  .-\__  '-'  ___/-. /                *
 *              ___'. .'  /--.--\  `. .'___              *
 *           ."" '<  `.___\_<|>_/___.' >' "".            *
 *          | | :  `- \`.;`\ _ /`;.`/ - ` : | |          *
 *          \  \ `_.   \_ __\ /__ _/   .-` /  /          *
 *      =====`-.____`.___ \_____/___.-`___.-'=====       *
 *                        `=---='                        *
 *      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      *
 *********__佛祖保佑__永无BUG__验收通过__钞票多多__*********
 *********************************************************/
package org.jbase.jbsc.commons.core.result;

import cn.hutool.core.bean.BeanUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Project: jbsc-commons-core <br/>
 * File: ResultBean.java <br/>
 * Class: org.jbase.jbsc.commons.core.result.ResultBean <br/>
 * Description: 通过接口、Rest、逻辑处理执行后的结果信息. <br/>
 * Copyright: Copyright (c) 2011 <br/>
 * Company: https://gitee.com/liuzp315 <br/>
 * Makedate: 2020/8/4 0:51 <br/>
 *
 * @author liupopo
 * @version 1.0
 * @since 1.0
 */
@ApiModel(description = "返回结果")
public class ResultBean<T> implements Serializable {
    private static final long serialVersionUID = 8862762193399136705L;

//    @SuppressWarnings("unchecked")
//    private Class<T> clazz() {
//        Class<T> targetClass = (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass())
//                .getActualTypeArguments()[0];
////        if(targetClass==null)
////            targetClass=Object.class;
//        return targetClass;
//    }
//
//    @SuppressWarnings("unchecked")
//    private T obj() {
//        T instance = null;
//        Class<T> targetClass = (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass())
//                .getActualTypeArguments()[1];
//        try {
//            instance = targetClass.newInstance();
//        } catch (InstantiationException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        return instance;
//    }

    @ApiModelProperty("对象创建的时间戳")
    private long timestamp = System.currentTimeMillis();

    public long getTimestamp() {
        return timestamp;
    }

    // 是否成功
    @ApiModelProperty("是否成功: true or false")
    private boolean success;

    // 消息 返回结果的说明
    @ApiModelProperty("返回结果的说明")
    private String msg;

    // 结果状态码
    @ApiModelProperty("结果状态码")
    private String code;

    // 数据
    @ApiModelProperty("业务数据")
    private T data;

    public ResultBean() {
    }

    public ResultBean(boolean success) {
        this.success = success;
    }

    public ResultBean(boolean success, String msg) {
        this.success = success;
        this.msg = msg;
    }

    public ResultBean(boolean success, String msg, String code) {
        this.success = success;
        this.msg = msg;
        this.code = code;
    }

    public ResultBean(T data) {
        this.success = true;
        this.data = data;
    }

    public ResultBean(String code, T data) {
        this.success = true;
        this.code = code;
        this.data = data;
    }

    public ResultBean(String code, String msg, T data) {
        this.success = true;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public boolean getSuccess() {
        return success;
    }

    public ResultBean<T> setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public ResultBean<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public String getCode() {
        return code;
    }

    public ResultBean<T> setCode(String code) {
        this.code = code;
        return this;
    }

    public T getData() {
//        if (this.data == null && this.map != null && !this.map.isEmpty()) {
////            Object t = new Object();
////            Object r = (Object)this.map;map
////            BeanUtil.fillBeanWithMap(this.map, t, false);
//            this.data = obj();
//            BeanUtil.copyProperties(this.map, this.data, true);
////            this.data = (T) t;
////            return t;
//        }
        return data;
    }

    public ResultBean<T> setData(T data) {
        this.data = data;
        return this;
    }

    public ResultBean<T> success() {
        this.setSuccess(true);
        this.setCode("200");
        this.setMsg("操作成功！");
        return this;
    }

    public ResultBean<T> fail() {
        this.setSuccess(false);
        this.setCode("500");
        this.setMsg("操作失败！");
        return this;
    }

    public static <T> ResultBean<T> fireSuccess() {
        ResultBean<T> rb = new ResultBean<T>();
        rb.setSuccess(true);
        rb.setCode("200");
        rb.setMsg("操作成功！");
        return rb;
    }

    public static <T> ResultBean<T> fireFail() {
        ResultBean<T> rb = new ResultBean<T>();
        rb.setSuccess(false);
        rb.setCode("500");
        rb.setMsg("操作失败！");
        return rb;
    }

//    @ApiModelProperty("无类型的业务数据对象")
////    @JsonIgnore
//    private Map<String, Object> map = Collections.emptyMap();
//
//    public ResultBean<T> clear() {
//        this.map = null;
//        return this;
//    }
//
//    public Object get(String key) {
//        if (this.map == null)
//            return null;
//        return this.map.get(key);
//    }
//
//    public ResultBean<T> remove(String key) {
//        if (this.map == null)
//            return this;
//        this.map.remove(key);
//        return this;
//    }
//
//    public ResultBean<T> put(String key, Object value) {
//        if (this.map == null)
//            this.map = new HashMap<>();
//        this.map.put(key, value);
//        return this;
//    }
//
//    public Map<String, Object> getMap() {
//        return this.map;
//    }
//
//    public ResultBean<T> setMap(Map<String, Object> map) {
//        this.map = map;
//        return this;
//    }
//
//    public ResultBean<T> putAll(Map<String, Object> map) {
//        if (this.map == null)
//            this.map = new HashMap<>();
//        this.map.putAll(map);
//        return this;
//    }

    /**
     * 设置返回code及msg
     *
     * @param codeMsg Code和Msg的枚举
     * @return
     */
    public ResultBean<T> setCode(IResultCodeMsg codeMsg) {
        this.code = codeMsg.getCode();
        this.msg = codeMsg.getMsg();
        return this;
    }

    /**
     * 返回失败信息，并指定结果code
     * @param codeMsg Code和Msg的枚举
     * @return
     */
    public ResultBean<T> fail(IResultCodeMsg codeMsg) {
        this.setSuccess(false);
        this.code = codeMsg.getCode();
        this.msg = codeMsg.getMsg();
        return this;
    }

}
